package cn.enine.tbcharge.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import cn.enine.tbcharge.adapter.CommonAdapter;
import cn.enine.tbcharge.adapter.ViewHolder;
import cn.enine.tbcharge.pojo.AppXmpy;
import cn.enine.tbcharge.pojo.RetMsg;
import cn.enine.tbcharge.util.HttpUrl;
import cn.enine.tbcharge.util.MyToastUtil;
import cn.enine.tbcharge.util.NetWorkUtils;
import cn.enine.tbcharge.util.SharedPreferencesUtils;
import cn.enine.tbcharge.util.UserClient;
import com.zyao89.view.zloading.ZLoadingDialog;
import com.zyao89.view.zloading.Z_TYPE;


import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import cn.enine.tbcharge.R;
import cn.enine.tbcharge.base.MyBaseActivity;
import cn.enine.tbcharge.view.XListView;

/**
 * 选择收费项目界面
 * Created by Administrator on 2017-11-14.
 */

public class ChooseXmActivity extends MyBaseActivity {


    List<AppXmpy> list;//信息列表
    XListView listView;
    ZLoadingDialog dialog;//加载动画
    Context con;
    private int pageNum1 = 1;// 无关键字页数
    EditText key;
    TextView title, uname;
    private ImageView cx;

    @Override
    protected void initUI() {
        setContentView(R.layout.xm_choose);
        cx = (ImageView) findViewById(R.id.cx);
        title = (TextView) findViewById(R.id.title);
        uname = (TextView) findViewById(R.id.uname);
        title.setText("选择收费项目");
        uname.setVisibility(View.INVISIBLE);
        con = ChooseXmActivity.this;
        key = (EditText) findViewById(R.id.key);
        listView = (XListView) findViewById(R.id.listView);
        listView.setPullLoadEnable(true);
        dialog = new ZLoadingDialog(con);
        dialog.setLoadingBuilder(Z_TYPE.DOUBLE_CIRCLE)//设置类型
                .setLoadingColor(Color.parseColor("#1C86EE"))//颜色
                .setHintText("Loading...")
                .setHintTextSize(16) // 设置字体大小 dp
                .setHintTextColor(Color.parseColor("#1C86EE"));  // 设置字体颜色
    }

    @Override
    protected void initData() {
        if (!NetWorkUtils.isNetworkConnected(con)) {//网络没有连接
            getAppXmpyNoNet();
        } else {
            getAppXmpy();
        }
       /* getAppXmpy();*/
    }

    @Override
    protected void initListener() {
        cx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getAppXmpy();
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(con, getIntent().getStringExtra("cname").getClass());
                Bundle b = new Bundle();
                b.putSerializable("appXmpy", list.get(i - 1));
                intent.putExtras(b);
                setResult(0, intent);
                finish();
            }
        });
    }

    private void getAppXmpyNoNet(){
        list = LitePal.findAll(AppXmpy.class);
        Log.i("本地list","size====="+list.size());
        listView.setPullLoadEnable(false);
        listView.setPullRefreshEnable(false);
        listView.setAdapter(new CommonAdapter(con, list, R.layout.xm_item) {
            @Override
            public void convert(ViewHolder helper, Object item) {
                if (item instanceof AppXmpy) {
                    AppXmpy appXmpyItem = (AppXmpy) item;
                    helper.setText(R.id.xmnumber, appXmpyItem.getYyxmbm());
                    helper.setText(R.id.xmname, appXmpyItem.getYyxmmc());
                    helper.setText(R.id.xmpy, appXmpyItem.getSzm());
                }
            }
        });
    }

    //更新收费项目信息
    //网络请求收费项目信息
    public void getAppXmpy() {
        RequestParams ps = new RequestParams();
        ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
        ps.add("type", "2");
        ps.add("page", pageNum1 + "");
        ps.add("flag", "0");
        if (key.getText().toString().length() != 0)
            ps.add("xmmc", key.getText().toString());

        UserClient.get(HttpUrl.GetXmList, ps, new AsyncHttpResponseHandler() {
            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onFailure(int statusCode, Throwable error, String content) {
                super.onFailure(statusCode, error, content);
                //MyToastUtil.ShowToast(con, "请求失败");
                getAppXmpyNoNet();
            }

            @Override
            public void onSuccess(String content) {
                super.onSuccess(content);
                dialog.dismiss();
                RetMsg ret = JSON.parseObject(content, RetMsg.class);
                if (ret.getCode().equals("200")) {
                    List<AppXmpy> appXmpyList = JSON.parseArray(ret.getList(), AppXmpy.class);
                    if (pageNum1 == 1)
                        list = new ArrayList<AppXmpy>();
                    for (int i = 0; i < appXmpyList.size(); i++) {
                        list.add(appXmpyList.get(i));
                    }
                    listView.setSelection(list.size() - appXmpyList.size());
                    listView.stopRefresh();
                    listView.stopLoadMore();
                    listView.setRefreshTime("刚刚");
                    listView.setXListViewListener(new XListView.IXListViewListener() {
                        @Override
                        public void onRefresh() {
                            pageNum1 = 1;
                            getAppXmpy();
                        }

                        @Override
                        public void onLoadMore() {
                            pageNum1 = pageNum1 + 1;
                            getAppXmpy();
                        }
                    });
                    listView.setAdapter(new CommonAdapter(con, list, R.layout.xm_item) {
                        @Override
                        public void convert(ViewHolder helper, Object item) {
                            if (item instanceof AppXmpy) {
                                AppXmpy appXmpyItem = (AppXmpy) item;
                                helper.setText(R.id.xmnumber, appXmpyItem.getYyxmbm());
                                helper.setText(R.id.xmname, appXmpyItem.getYyxmmc());
                                helper.setText(R.id.xmpy, appXmpyItem.getSzm());
                            }
                        }
                    });
                    ////////////////////////////////////////////////////////////////////////本地化
//                    for (int i = 0; i < appXmpyList.size(); i++) {
//                        AppXmpy fwq = appXmpyList.get(i);
//                        List<AppXmpy> bdlist = LitePal.where("NID = ?", fwq.getNid()).find(AppXmpy.class);
//                        if(null == bdlist || bdlist.size()<=0){
//                            //本地不存在，直接写入
//                            appXmpyList.get(i).setIsupdate(1);//设置不需要更新的操作
//                            appXmpyList.get(i).save();
//                        }else{
//                            //本地已存在
//                            if(fwq.getIsupdate()==1){
//                                //需要更新的数据
//                                bdlist.get(0).delete();
//                                appXmpyList.get(i).setIsupdate(1);//设置不需要更新的操作
//                                appXmpyList.get(i).save();
//                            }
//                        }
//                    }
                    ////////////////////////////////////////////////////////////////////////本地化
                } else {
                    //MyToastUtil.ShowToast(con, "获取错误");
                    getAppXmpyNoNet();
                }
            }
        });
    }
}
